<?php  namespace App\Api\Models;

use Input;
class Orders extends BaseModel{
    
	//表明
	protected $table = 'orders';
	//主键
	protected $primaryKey = 'id';
        
        
    //订单列表
    public function getOrderlist() {
        $mobile = Input::get('mobile');
        if(!$mobile){
            $retArr["msg"]= "请传入手机号";
            $retArr["result"] = 0;
            return $retArr;
        }
        $user = User::select('*')
                ->where('phone','=',$mobile)
                ->where('status','=',1)
                ->where('is_delete','=',0)
                ->first();
        if($user){
            $orderList = Orders::select('*')
                    ->where('user_id','=',$user->id)
                    ->where('status','=',1)
                    ->where('is_delete','=',0)
                    ->get();
            $data['data']=[];
            if($orderList){
                foreach ($orderList as $val){
                    $temp = [];
                    $temp['id'] = $val->id;
                    $temp['userId'] = $val->user_id;
                    $temp['wxorder'] = $val->wxorder;
                    $temp['clientId'] = $val->client_id;
                    $temp['goodsId'] = $val->goods_id;
                    $temp['amount'] = $val->amount;
                    switch ($val->type){
                        case 1:
                            $type = "消费";
                            break;
                        case 2:
                            $type = "交押金";
                            break;
                        case 3:
                            $type = "退押金";
                            break;
                        case 4:
                            $type = "充值";
                            break;
                        case 5:
                            $type = "提现";
                            break;
                        default :
                            $type = "消费";
                            break;
                    }
                    $temp['type'] = $type;
                    $pay_type = "微信支付";
                    if($val->pay_type == 1){
                        $pay_type = "微信支付";
                    }else if($val->pay_type == 2){
                        $pay_type = "线下支付";
                    }
                    $temp['pay_type'] = $pay_type;
                    $data['data'][] = $temp;
                }
            }
            $retArr["msg"]= "获取成功";
            $retArr["result"] = 1;
            return array_merge($retArr,$data);
        }
    }
    //订单详情
    public function getOrderinfo() {
        $mobile = Input::get('mobile');
        $orderId = Input::get('orderId');
        if(!$orderId){
            $retArr["msg"]= "请输入订单号";
            $retArr["result"] = 0;
            return $retArr;
        }
        if(!$mobile){
            $retArr["msg"]= "请传入手机号";
            $retArr["result"] = 0;
            return $retArr;
        }
        $order = Orders::select('*')
                ->where('id','=',$orderId)
                ->where('status','=',1)
                ->where('is_delete','=',0)
                ->first();
        $temp = [];
        if($order){
            $temp['id'] = $val->id;
            $temp['userId'] = $val->user_id;
            $temp['wxorder'] = $val->wxorder;
            $temp['clientId'] = $val->client_id;
            $temp['goodsId'] = $val->goods_id;
            $temp['amount'] = $val->amount;
            switch ($val->type){
                case 1:
                    $type = "消费";
                    break;
                case 2:
                    $type = "交押金";
                    break;
                case 3:
                    $type = "退押金";
                    break;
                case 4:
                    $type = "充值";
                    break;
                case 5:
                    $type = "提现";
                    break;
                default :
                    $type = "消费";
                    break;
            }
            $temp['type'] = $type;
            $pay_type = "微信支付";
            if($val->pay_type == 1){
                $pay_type = "微信支付";
            }else if($val->pay_type == 2){
                $pay_type = "线下支付";
            }
            $temp['pay_type'] = $pay_type;
        }
        $retArr["msg"]= "获取成功";
        $retArr["result"] = 1;
        return array_merge($retArr,$temp);
        
    }
    //订单创建
    public function Order() {
        $mobile = Input::get('mobile');
        $wxorder = Input::get('wxorder');
        $orderId = Input::get('eno');
        $orderId = Input::get('bno');
        $amount = Input::get('amount');
        $type = Input::get('type',1);
        //pay_type =1
        if(!$mobile){
            $retArr["msg"]= "请传入手机号";
            $retArr["result"] = 0;
            return $retArr;
        }
        $user = User::select('*')
                ->where('phone','=',$mobile)
                ->where('status','=',1)
                ->where('is_delete','=',0)
                ->first();
        if($user){
            $box = CabinetCell::select('*')
                ->where('cabinet_id','=',$eno)
                ->where('id','=',$bno)
                ->where('is_delete','=',0)
                ->first();
            $order = new Orders();
            $order->wxorder = $wxorder;
            if($user->is_bind == 2){
                $order->client_id = $user->client_id;
            }
            $order->goods_id = $box->goods_id;
            $order->amount = $amount;
            $order->type = 1;
            $order->cabinet_id = $eno;
            $order->cell_id = $bno;
            $order->pay_type = 1;
            $order->status = 1;
            $order->save();
            $retArr["msg"]= "创建成功";
            $retArr["result"] = 1;
            return $retArr;
        }
    }
    //订单创建信息返回
    public function OrderInfo() {
        
        
    }

}